﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project.WinForms
{
    public partial class TransportCreatorForm : Form
    {


        public AbstractTransport transport;

        private Dictionary<string, Type> types = new Dictionary<string, Type>();

        public TransportCreatorForm()
        {
            InitializeComponent();

            types.Add("Самолет", typeof(Plane));
            types.Add("Вертолет", typeof(Helicopter));
            types.Add("Легковой автомобиль", typeof(PassengerCar));
            types.Add("Грузовик", typeof(Truck));

            typeComboBox.SelectedIndex = 0;


        }

        private void AddButton_Click_1(object sender, EventArgs e)
        {
            string typename = typeComboBox.Text;
            Type type = types[typename];

            uint result = 0;

            transport = (AbstractTransport)type.GetConstructor(new Type[] { }).Invoke(new object[] { });
            transport.SetName(NameTextBox.Text);

            transport.SetFare(uint.TryParse(FareTextBox.Text, out result) ? uint.Parse(FareTextBox.Text) : result);

            transport.SetName(NameTextBox.Text);

            if (typeof(Plane).IsAssignableFrom(type))
            {
                Plane plane = (Plane)transport;
                plane.SetMaxRange(uint.TryParse(RangeTextBox.Text, out result) ? uint.Parse(RangeTextBox.Text) : result);
                plane.SetNumberOfPropellers(uint.TryParse(PropellersTextBox.Text, out result) ? uint.Parse(PropellersTextBox.Text) : result);
            }
            if (typeof(Helicopter).IsAssignableFrom(type))
            {
                Helicopter helicopter = (Helicopter)transport;
                helicopter.SetMaxRange(uint.TryParse(RangeTextBox.Text, out result) ? uint.Parse(RangeTextBox.Text) : result);
                helicopter.SetNumberOfRotors(uint.TryParse(RotorsTextBox.Text, out result) ? uint.Parse(RotorsTextBox.Text) : result);
            }

            if (typeof(PassengerCar).IsAssignableFrom(type))
            {
                PassengerCar car = (PassengerCar)transport;
                car.SetNumberOfWheels(uint.TryParse(WheelsTextBox.Text, out result) ? uint.Parse(WheelsTextBox.Text) : result);
                car.SetNumberOfSeats(uint.TryParse(SeatsTextBox.Text, out result) ? uint.Parse(SeatsTextBox.Text) : result);
            }

            if (typeof(Truck).IsAssignableFrom(type))
            {
                Truck truck = (Truck)transport;
                truck.SetNumberOfWheels(uint.TryParse(WheelsTextBox.Text, out result) ? uint.Parse(WheelsTextBox.Text) : result);
                truck.SetCapacity(uint.TryParse(CapacityTextBox.Text, out result) ? uint.Parse(CapacityTextBox.Text) : result);
            }

            this.Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            NameTextBox.Text = "";
            FareTextBox.Text = "";
            RangeTextBox.Text = "";
            PropellersTextBox.Text = "";
            RotorsTextBox.Text = "";
            WheelsTextBox.Text = "";
            SeatsTextBox.Text = "";
            CapacityTextBox.Text = "";
        }

        private void Reset()
        {
            RangeTextBox.Enabled = false;
            PropellersTextBox.Enabled = false;
            RotorsTextBox.Enabled = false;
            WheelsTextBox.Enabled = false;
            SeatsTextBox.Enabled = false;
            CapacityTextBox.Enabled = false;
        }

        private void typeComboBox_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            Reset();

            switch (typeComboBox.Text)
            { 
                case "Самолет":
                    RangeTextBox.Enabled = true;
                    PropellersTextBox.Enabled = true;
                    break;
                case "Вертолет":
                    RangeTextBox.Enabled = true;
                    RotorsTextBox.Enabled = true;
                    break;
                case "Легковой автомобиль":
                    WheelsTextBox.Enabled = true;
                    SeatsTextBox.Enabled = true;
                    break;
                case "Грузовик":
                    WheelsTextBox.Enabled = true;
                    CapacityTextBox.Enabled = true;
                    break;
            }

        }

        private void CancelButton_Click_1(object sender, EventArgs e)
        {
            NameTextBox.Text = "";
            FareTextBox.Text = "";
            RangeTextBox.Text = "";
            PropellersTextBox.Text = "";
            RotorsTextBox.Text = "";
            WheelsTextBox.Text = "";
            SeatsTextBox.Text = "";
            CapacityTextBox.Text = "";
        }
    }
}
